<template>
    <div>
        <!-- Breadcrumb 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
        <!-- 搜索 -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input placeholder="根据登录名模糊查询" v-model="queryInfo.query">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUsersList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true" >新增用户</el-button>
        </el-col>
          <el-col :span="4">
          <el-button type="success" @click="editDialogVisible=true" >查询用户</el-button>
        </el-col>
          <el-col :span="4">
          <el-button type="danger" @click="removeBatch(sels)">批量删除</el-button>
        </el-col>
      </el-row>
    <!-- card展示 -->
    <el-card>
 <!-- 数据表格 -->
      <el-table @selection-change="selsChange" :data="userList" border stripe>
        <!-- 为表格添加一个编号（索引） -->
          <el-table-column  type="selection" width="65" ></el-table-column>
        <el-table-column type="index" label="编号"> </el-table-column>
        <el-table-column  prop="username" label="登录名"></el-table-column>
        <el-table-column  prop="realname" label="真实姓名"> </el-table-column>
        <el-table-column prop="deptid" label="用户所在科室" :formatter="deptidFormat"> </el-table-column>
        <el-table-column prop="usetype"  label="用户类别" :formatter="usetypeFormat"> </el-table-column>
         <el-table-column  prop="doctitleid" label="医生职称" :formatter="docTitleFormat"> </el-table-column>
        <el-table-column width="200px" label="操作">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini" 
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-tooltip
              effect="dark"
              content="删除信息"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removeUserById(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
        <!-- Pagination分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

     <!-- 查询用户的对话框 -->
    <el-dialog
      title="查询用户"
      :visible.sync="editDialogVisible"
      width="80%"
    >
      <el-form
        :model="qyertForm"
        label-width="100px"
      >
        <el-form-item label="登录名">
          <el-input v-model="qyertForm.userName" prop="userName" ></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="qyertForm.realName"></el-input>
        </el-form-item>
        <el-form-item label="用户所在科室" prop="deptId">
          <el-input v-model="qyertForm.deptId"></el-input>
        </el-form-item>
        <el-form-item label="用户类别" prop="useType">
          <el-input v-model="qyertForm.useType"></el-input>
        </el-form-item>
          <el-form-item label="医生职称" prop="docTitleId">
          <el-input v-model="qyertForm.docTitleId"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click=" editDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="queryUser">查询</el-button>
      </span>
    </el-dialog>

    <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%">
      <!-- 创建一个表单 -->
      <!-- 用户名 密码  真实姓名 用户类型 医生职称 用户所在科类-->
      <!-- 写完表单后要添加数据绑定以及检验的规则 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef">
        <el-form-item prop="username" label="用户名">
          <el-input
            v-model="addForm.username"
            prefix-icon="iconfont icon-user"
            placeholder="请填写账号信息"
          ></el-input>
        </el-form-item>
        <el-form-item prop="passWord" label="密码">
          <el-input
            type="password"
            v-model="addForm.passWord"
            prefix-icon="iconfont icon-user"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="realName" label="真实姓名">
          <el-input
            v-model="addForm.realName"
            prefix-icon="iconfont icon-user"
            placeholder="请输真实姓名"
          ></el-input>
        </el-form-item>
        <!-- 用户所在科室  下拉列表 -->
        <el-form-item prop="deptId" label="用户所在科室">
         <el-select v-model="addForm.deptId" placeholder="请选择">
    <el-option
      v-for="item in deptNameList"
      :key="item.id"
      :label="item.deptname"
      :value="item.id">
    </el-option>
  </el-select>
        </el-form-item>
        <!-- 用户类别下拉列表框 -->
        <el-form-item prop="userType" label="用户类别">
         <el-select v-model="addForm.userType" placeholder="请选择">
         <el-option
         v-for="item in useroptions"
         :key="item.value"
         :label="item.label"
         :value="item.value">
        </el-option>
  </el-select>
        </el-form-item>
        <!-- 医生职称下拉列表框 -->
         <el-form-item prop="docTitleId" label="医生职称">
          <el-select v-model="addForm.docTitleId" placeholder="请选择">
    <el-option
      v-for="item in docoptions"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd">取 消</el-button>
        <el-button type="primary" @click="sureAdd">确 定</el-button>
      </span>
    </el-dialog>

       <!-- 查询用户的对话框 -->
    <el-dialog
      title="查询用户"
      :visible.sync="editDialogVisible"
      width="80%"
    >
      <el-form
        :model="qyertForm"
        label-width="100px"
      >
        <el-form-item label="登录名">
          <el-input v-model="qyertForm.userName" prop="userName" ></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="qyertForm.realName"></el-input>
        </el-form-item>
        <el-form-item label="用户所在科室" prop="deptId">
          <el-input v-model="qyertForm.deptId"></el-input>
        </el-form-item>
        <el-form-item label="用户类别" prop="useType">
          <el-input v-model="qyertForm.useType"></el-input>
        </el-form-item>
          <el-form-item label="医生职称" prop="docTitleId">
          <el-input v-model="qyertForm.docTitleId"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click=" editDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="queryUser">查询</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改用户" :visible.sync="updateDialogVisible" width="50%">
      <!-- 创建一个表单 -->
      <!-- 用户名 密码  真实姓名 用户类型 医生职称 用户所在科类-->
      <!-- 写完表单后要修改数据绑定以及检验的规则 -->
      <el-form :model="updateForm" :rules="updateFormRules" ref="updateFormRef">
        <el-form-item prop="username" label="用户名">
          <el-input
            v-model="updateForm.username"
            prefix-icon="iconfont icon-user"
            placeholder="请填写账号信息"
          ></el-input>
        </el-form-item>
        <el-form-item prop="passWord" label="密码">
          <el-input
            type="password"
            v-model="updateForm.passWord"
            prefix-icon="iconfont icon-user"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="realName" label="真实姓名">
          <el-input
            v-model="updateForm.realName"
            prefix-icon="iconfont icon-user"
            placeholder="请输真实姓名"
          >{{updateForm.realName}}</el-input>
        </el-form-item>
        <!-- 用户所在科室  下拉列表 -->
        <el-form-item prop="deptId" label="用户所在科室">
         <el-select v-model="updateForm.deptId" placeholder="请选择">
    <el-option
      v-for="item in deptNameList"
      :key="item.id"
      :label="item.deptname"
      :value="item.id">
    </el-option>
  </el-select>
        </el-form-item>
        <!-- 用户类别下拉列表框 -->
        <el-form-item prop="userType" label="用户类别">
         <el-select v-model="updateForm.userType" placeholder="请选择">
         <el-option
         v-for="item in useroptions"
         :key="item.value"
         :label="item.label"
         :value="item.value">
        </el-option>
  </el-select>
        </el-form-item>
        <!-- 医生职称下拉列表框 -->
         <el-form-item prop="docTitleId" label="医生职称">
          <el-select v-model="updateForm.docTitleId" placeholder="请选择">
    <el-option
      v-for="item in docoptions"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="sureUpdate">确 定</el-button>
      </span>
    </el-dialog>

    </div>
</template>

<script>
export default {
    data(){
      return{
       queryInfo:{
          query:"",
          pageNum:1,
          pageSize:2
        },
        //根据条件查询提交的条件
        qyertForm:{
            userName:'',
            realName:'',
            useType:'',
            docTitleId:'',
            deptId:'',
            pageNum:1,
            pageSize:2
        },
        //添加用户的表单数据addForm
        addForm:{
          username:'',
          realName:'',
          passWord:'',
          deptId:'',
          userType:'',
          docTitleId:''
        },
        //修改用户的表单数据updateForm
         updateForm:{
           userId:null,
          username:'a',
          realName:'',
          passWord:'',
          deptId:0,
          userType:0,
          docTitleId:0
        },
        //添加用户 用户类别下拉列表
          useroptions: [{
          value:  1,
          label: '医院管理员'
        }, {
          value: 2,
          label: '挂号收费员'
        }, {
          value: 3,
          label: '门诊医生'
        }, {
          value: 4,
          label: '医技医生'
        }, {
          value: 5,
          label: '药房操作员'
        },
        {
          value: 6,
          label: '财务管理员'
        }],
        docoptions:[
          {
          value: 81,
          label: '主任医师'
        }, {
          value: 82,
          label: '副主任医师'
        }, {
          value: 83,
          label: '主治医师'
        }, {
          value: 84,
          label: '住院医师'
        }, 
        ],
        //添加用户表单验证 addFormRules
        total:0,
         //保存用户的数据
      userList: [],
        //替换数据  用户所在科数组
      deptNameList:[],
      //添加Dilog框默认关闭
      addDialogVisible:false,
      //查询Dilog框默认关闭
      editDialogVisible:false,
      //修改Dilog框默认关闭
      updateDialogVisible:false,
      //用户类型数组
      userTypeName:[
        "医院管理员","挂号收费员","门诊医生","医技一声","药房操作员","财务管理员"
      ],
      //医生职称数组
      docTitleList:["无","主任医师","副主任医师","主治医师","住院医师"],
      //添加信息表单验证
      addFormRules:{
        username:[
           { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名的长度在3~10个字符之间",
            trigger: "blur"
          }],
        passWord: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "密码的长度在6~15个字符之间",
            trigger: "blur"
          }
        ],
         realName:[
            { required: true, message: "请输真实姓名", trigger: "blur" },
         
         ],
         deptId:[  { required: true, message: "请输真实姓名", trigger: "blur" }]
        
      },
      //修改信息表单验证
      updateFormRules:{
        username:[
           { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名的长度在3~10个字符之间",
            trigger: "blur"
          }],
        passWord: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "密码的长度在6~15个字符之间",
            trigger: "blur"
          }
        ],
         realName:[
            { required: true, message: "请输真实姓名", trigger: "blur" },
         
         ],
        //  deptId:[  { required: true, message: "请输真实姓名", trigger: "blur" }]
        
      },
      //复选框id数组
      sels:[],
      }
    },
     created() {
    this.getUsersList();
    this.getDeptNameList();
  },
    methods:{
      //查询所有数据
     async getUsersList(){
         //向后台发送请求获取数据
      const { data: res } = await this.$http.get("user/findUserByPage",
      { params: this.queryInfo}
      );
      // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("获取用户信息失败");
      }
      // 如果状态正常，获取用户数据
      this.userList = res.user;
      this.total = res.total;
      console.log(res);
    },
    //获取DeptNameList数组的方法
    async getDeptNameList(){
     const {data:res}=await this.$http.get(`department/queryDeptName`, 
      { params: this.queryInfo});
      // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("获取医生职称信息失败");
      }
    this.deptNameList=res.list;
    console.log(res);
    },
   handleSizeChange(size) {
      // console.log(size)
      this.queryInfo.pageSize = size;
      //重新查询用户信息
      this.getUsersList();
    },
    handleCurrentChange(page) {
      // console.log(page)
      this.queryInfo.pageNum = page;
      //重新查询用户信息
      this.getUsersList();
    },
     //根据条件查询用户信息
    async queryUser(){
     const { data: res } = await this.$http.get("user/queryUser",{params:this.qyertForm});
     //如果状态异常
      // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("获取用户信息失败");
      }
       // 如果状态正常，获取用户数据
        this.userList = res.user;
      this.total = res.total;
      console.log(res);
       this.editDialogVisiblep=false
    },
    
    //替换数据 用户类型
    usetypeFormat(row){
      let index = row.usetype;
     
      return this.userTypeName[index-1];
    },
     //替换数据 用户所在科室
    deptidFormat(row){
       let deptName;
       this.deptNameList.forEach(value=>{
      if(row.deptid==value.id){
        deptName=value.deptname
    }
       })
      return deptName
     },
     //替换数据 医生职称
     docTitleFormat(row){
       let index=row.doctitleid
        if(index==1){
        index=80
      }
       return this.docTitleList[index-80]
     },


    //删除用户信息
    async removeUserById(row){
     const{data:res}=await this.$http.get(`user/deleteUserById`, {
        params: {id:row}
      })
       // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户信息失败");
      }
       this.getUsersList();
    },

    //添加信息 取消
    cancelAdd(){           
      this.$refs.addFormRef.resetFields();
      this.addDialogVisible = false;
    },

    //添加信息 确定
   async sureAdd(){
     const {data:res}= await this.$http.post(`user/insertUser`, 
       this.addForm
      )
        // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户信息失败");
      }
     this.getDeptNameList();
     this.$refs.addFormRef.resetFields();
       this.addDialogVisible=false;
        this.getUsersList();
    },
    //批量删除 复选框 选中id放进数组
    selsChange(sels){
     this.sels=sels
    } ,

    //批量删除 向后台发送请求
  async removeBatch(rows){
  // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)

      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

    var ids = [];
    rows.forEach(element=>{
      ids.push(element.id)
    })

    const{data:res} =await this.$http.post(`user/deleteByChoose`,{info:ids});
     // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户信息失败");
      }
      this.getUsersList();
 },

    //打开修改用户的Dilog窗口
   async showEditDialog(row){
      this.updateDialogVisible=true;  
      //根据id获取用户信息
      const{data:res}=await this.$http.get(`user/selectUserById`,{  params: {id:row}})
        // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("根据id用户信息失败");}
        console.log(res)
             this.updateForm.username=res.user.username
             this.updateForm.passWord=res.user.password
             this.updateForm.realName=res.user.realname
             this.updateForm.docTitleId=res.user.doctitleid
             this.updateForm.deptId=res.user.deptid
             this.updateForm.userType=res.user.usetype
              this.updateForm.userId=row
             
      
       },
       //发送 修改用户  请求
    async sureUpdate(){
       const{data:res}=await this.$http.post(`user/updateUser`,  this.updateForm);
       if (res.meta.status !== 200) {
        return this.$message.error("修改用户信息失败");
      }
       this.getDeptNameList();
       this.$refs.updateFormRef.resetFields();
        this.getUsersList();
          this.updateDialogVisible=false;
    }
    }
}
</script>

<style lang="less" scoped>

</style>